package com.trabpoo.megamanxrunner;

import javax.swing.ImageIcon;

import java.awt.Rectangle;
public class Megaman extends GameElement{
	//Possíveis estados do personagem.
	private int iP, gH;
	
	
	private int state;
	public static final int STATE_RUNNING = 1;
	public static final int STATE_JUMPING = 2;
	public static final int STATE_DIE = 3;
	public static final int STATE_DASH = 4;
	public static final int STATE_BEGIN = 5;
	public static final int STATE_DEAD = 6;
	public static final int STATE_SHOOTING = 7;
	public int dx,dy,x=0;
	
	private boolean isJumping = false;
	private boolean isRunning = false;
	private boolean isDying = false;
	private boolean isDashing = false;
	private boolean isBeginning = false;
	public boolean isDead = false;
	public boolean isShooting = false;
	public boolean isFiring = false;
	public boolean STOP = false;
	
	//Atributos responsáveis pelo manuseio das sprites
	private int cena;
	private ImageIcon jumping[] = new ImageIcon[7];
	private ImageIcon running[] = new ImageIcon[10];
	private ImageIcon dash[] = new ImageIcon[2];
	private ImageIcon die[] = new ImageIcon[12]; 
	private ImageIcon begin[] = new ImageIcon[10];
	private ImageIcon shooting[] = new ImageIcon[7];
	
	
	Missiles missiles;
	
	//Atributos responsáveis pelo posicionamento dos sprites na tela
	private int runningBaseImageWidth;
	private int runningBaseImageHeight;
	private int dashBaseImageHeight;
	private int dieBaseImageHeight;
	private int beginBaseImageHeight;
	private int shootingBaseImageHeight;
	
	private int d = 0;
	private final int dashTime = 5;
	//private int posY, velY;
	private final int gravity = 4;
	private final int jumpSpeed = 24;
	private Rectangle hitBox;
	InitializeSprite is;
	public Megaman(int wWidth, int wHeight, int i, int g){
		initializeSprites();
		
		iP = i;
		gH = g;
		
		state = STATE_BEGIN;
		setPosY(getPosY() - wHeight/2);
		setVelY(20);
		setPosX(iP);
		
	}
	
	private void initializeSprites(){
		is = new InitializeSprite();
		if(Settings.character == Settings.MEG_X_FULLARMOR){
			jumping = is.initializeJumpingX();
			running = is.initializeRunningX();	
			dash = is.initializeDashingX();
			die = is.initializeDyingX();
			begin = is.initializeBegginingX();
			shooting = is.initializeShottingX();
		}
		if(Settings.character == Settings.ZERO){
			jumping = is.initializeJumpingZERO();
			running = is.initializeRunningZERO();	
			dash = is.initializeDashingZERO();
			die = is.initializeDyingZERO();
			begin = is.initializeBegginingZERO();
			shooting = is.initializeShottingZERO();
		}
		this.runningBaseImageWidth = running[0].getImage().getWidth(null);
		this.runningBaseImageHeight = running[0].getImage().getHeight(null);
		this.dashBaseImageHeight = dash[1].getImage().getHeight(null);
		this.beginBaseImageHeight = begin[9].getImage().getHeight(null);
		this.dieBaseImageHeight = die[0].getImage().getHeight(null);
		this.shootingBaseImageHeight = shooting[0].getImage().getHeight(null);
	}
	
	
	public void setState(int st){
		this.state = st;
	}
	
	public int getState(){
		return state;
	}
	
	public void sleep(int millis) {
	    try {
	       Thread.sleep(millis);
	   } catch (InterruptedException e) {}
	}
	
	public ImageIcon getIcon(){
		if(isRunning){
			sleep(10);
			cena++;
			if(cena >= running.length){
				cena = 0;
			}
			return running[cena];
		}
		if(isJumping){//Existem tres fases no pulo, para tres cenas diferentes
			if(getVelY() < 0){
				if (cena <= 1){//Quando o megaman ainda está subindo
					cena ++;
				}
			}
			else{
				if(getPosY() < 0){//Quando está descendo, mas ainda não tocou o chão.
					if(cena <= 3){
						cena++;
					}
				}
				else{//Quando ele toca o chao.
					if(cena <= 6){
						cena++;
					}
				}
			}
			sleep(15);
			return jumping[cena];
		}
		if(isDashing){
			return dash[cena];
		}
		if (isDying){
				cena++;
				sleep(50);
				return die[cena];
		}
		
		if (state == STATE_BEGIN){
			if(getPosY() >=0){
				setPosY(0);
				setVelY(0);
				cena++;
				return begin[cena];
			}
			else{
				return begin[cena];
			}
		}
		if(isDead){
			return die[cena];
		}
		if(isShooting){
			sleep(20);
			return shooting[cena];
		}
		return null;
	}
	public int setSpriteWidthPosition(int x){
		if(state == STATE_RUNNING){
			x = x - (running[cena].getImage().getWidth(null) - this.runningBaseImageWidth)/2;
			return x; 
		}
		else if(state == STATE_BEGIN){
			x = x - (begin[cena].getImage().getWidth(null) - this.runningBaseImageWidth)/2;
			return x; 
		}
		return x;
	}
	public int setSpriteHeightPosition(int y){
		if(state == STATE_RUNNING){
			y = y + (running[cena].getImage().getHeight(null) - this.runningBaseImageHeight)/2;
		}
		if(state == STATE_DASH){
			y = y + (runningBaseImageHeight - dashBaseImageHeight) + 3;
		}
		if (state == STATE_DIE){
			if(cena != 11){
				y = y + (runningBaseImageHeight - dieBaseImageHeight) + 2;
			}
			else{
				y = y + (runningBaseImageHeight - dieBaseImageHeight) + 15;
			}
		}
		if (state == STATE_BEGIN){
			y = y + (runningBaseImageHeight - beginBaseImageHeight);
		}
		if(state == STATE_DEAD){
			y = y + 15;
		}
		if(state == STATE_SHOOTING){
			y = y + (runningBaseImageHeight - shootingBaseImageHeight);
		}
		
		return y;
	}
	public int getSpriteWidth(){
		if(isRunning){
			return running[cena].getImage().getWidth(null);
		}
		if(isJumping){
			return jumping[cena].getImage().getWidth(null);
		}
		if(isDashing){
			return dash[cena].getImage().getWidth(null);
		}
		if (isDying){
			return die[cena].getImage().getWidth(null);
		}
		if(isBeginning){
			return begin[cena].getImage().getWidth(null);
		}
		if(isDead){
			return die[cena].getImage().getWidth(null);
		}
		if(isShooting){
			return shooting[cena].getImage().getWidth(null);
		}
		return 0;

	}
	public int getSpriteHeight(){
		if(isRunning){
			return running[cena].getImage().getHeight(null);
		}
		if(isJumping){
			return jumping[cena].getImage().getHeight(null);
		}
		if(isDashing){
			return dash[cena].getImage().getHeight(null);
		}
		if(isDying){
			return die[cena].getImage().getHeight(null);
		}
		if(isBeginning){
			return begin[cena].getImage().getHeight(null);
		}
		if(isDead){
			return die[cena].getImage().getHeight(null);
		}
		if(isShooting){
			return shooting[cena].getImage().getHeight(null);
		}
		return 0;

	}
	private void setHitBox(){
		if(Settings.getCharacter() == Settings.ZERO){
		hitBox = new Rectangle(setSpriteWidthPosition(iP) - getSpriteWidth()+getMove() - 20, //posicionamento da imagem
				setSpriteHeightPosition(gH) - getSpriteHeight() + getPosY() - 15,//posicionamento da imagem
				getSpriteWidth()*2 - 20,//tamanho da imagem
				getSpriteHeight()*2 - 15);//tamanho da imagem)
		}
		hitBox = new Rectangle(setSpriteWidthPosition(iP) - getSpriteWidth()+getMove(), //posicionamento da imagem
				setSpriteHeightPosition(gH) - getSpriteHeight() + getPosY(),//posicionamento da imagem
				getSpriteWidth()*2,//tamanho da imagem
				getSpriteHeight()*2 );//tamanho da imagem)
	}
	public Rectangle getHitBox(){
		return hitBox;
	}
	public void run(){
		if(!isRunning){
			cena = 0;
			state = STATE_RUNNING;
			isRunning = true;
		}
	}
	public void jump(){
		if(!isJumping){
			isRunning = false;
			cena = 0;
			setVelY(-this.jumpSpeed);	
			isJumping = true;
		}
		else{
			if(getPosY() >= 0){
				setVelY(0);
				isJumping = false;
				setPosY(0);
				run();
			}
			else{
				setVelY(getVelY() + gravity);
			}
		}
	}
	public void dash(){
		if(!isDashing){
			cena = 0;
			d = 0;
			isDashing = true;
			isRunning = false;
		}
		else{
			if(cena < 1 && d < dashTime){
				cena++;
				sleep(25);
			}
			else{
				if(d < dashTime){
					d++;
					sleep(15);
				}
				else{
					if(cena > 0){
						cena--;
						sleep(20);
					}
					else{
						isDashing = false;
						run();
					}
				}
			}
		}
	}
	private void die(){
		if(!isDying){
			cena = 0;
			isDying = true;
			isRunning = false;
			isJumping = false;
			isDashing = false;
			setVelY(0);
		}
		if (cena == 11){
			isDying = false;
			state = STATE_DEAD;
			cena = 11;
			setVelX(GamePlay.velocidade);
			isDead = true;
			setPosY(0);
		}
	}
	private void begin(){
		if(!isBeginning){
			isBeginning = true;
			cena = 0;
		}
		if (cena == 9){
			isBeginning = false;
			run();
		}
	}
	private void shoot(){
		cena++;
		if(!isShooting){
			isShooting = true;
			isRunning = false;
			cena = 0;
		}
		if (cena > 5){
			sleep(15);
			cena = 0;
			isShooting = false;
			run();
		}
		cena++;
	}
	
	public void update(){
		if(state == STATE_JUMPING){
			jump();
		}
		if(state == STATE_DASH){
			dash();
		}
		if(state == STATE_DIE){
			die();
		}
		if(state == STATE_BEGIN){
			begin();
		}
		if(state == STATE_SHOOTING){
			shoot();
		}
		setPosY(getPosY() + getVelY());
		setPosX(getPosX() + getVelX());
		setHitBox();
	}

	
	public void fire(){
		isFiring = true;
		
	}
	
	public int getMove(){
		return x;
	}
	public void move(){
		x += dx;
	}
	

	
}
